package com.qdxwx.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public enum TKClientBrowser {

    OUTLOOK("Outlook 2010", new String[]{"MSOffice", "Microsoft Outlook"}, new String[]{"MSOffice ([0-9]+))", "Microsoft Outlook ([0-9]+)"}),
    OUTLOOK_EXPRESS7("Windows Live Mail", new String[]{"Outlook-Express/7.0"}, null),

    IE_MOBILE("IE Mobile", new String[]{"IEMobile"}, new String[]{"IEMobile[ |/]([0-9]+)"}),
    IE_XBOX("Xbox", new String[]{"xbox"}, new String[0]),
    IE("Internet Explorer", new String[]{"MSIE", "Trident", "IE "}, new String[]{"rv:([0-9]+)","IE ([0-9]+)"}),

    EDGE("Microsoft Edge", new String[]{"Edge/", "Edg/"}, new String[]{"Edg/([0-9]+)", "Edge/([0-9]+)"}, new String[]{"Mobile"}),
    EDGE_MOBILE("Microsoft Edge Mobile", new String[]{"Edge/", "Edg/"}, new String[]{"Edg/([0-9]+)", "Edge/([0-9]+)"}),

    CHROME("Chrome", new String[]{"Chrome", "CrMo", "CriOS"}, new String[]{"Chrome/([0-9]+)"}, new String[]{"Mobile"}),
    CHROME_MOBILE("Chrome Mobile", new String[]{"CrMo", "CriOS", "Mobile Safari"}, new String[]{"Chrome/([0-9]+)", "OPR/([0-9]+)"}),

    FIREFOX("Firefox", new String[]{"Firefox", "FxiOS"}, new String[]{"Firefox/([0-9]+)"}, new String[]{"Mobile"}),
    FIREFOX_MOBILE("Firefox Mobile", new String[]{"Firefox"}, new String[]{"Firefox/([0-9]+)"}),

    MOBILE_SAFARI("Mobile Safari", new String[]{"Mobile Safari"}, new String[]{"Version/([0-9]+)"}),
    SAFARI("Safari", new String[]{"Safari"}, new String[]{"Version/([0-9]+)"}),

    OPERA_MINI("Opera Mini", new String[]{"Opera Mini"}, null),
    OPERA("Opera", new String[]{"OPR/", "Opera", "Coast/"}, new String[]{"OPR/([0-9]+)", "Opera/([0-9]+)", "Version/([0-9]+)"}, new String[]{"Mobile"}),
    OPERA_MOBILE("Opera Mobile", new String[]{"OPR/", "Opera", "Coast/"}, new String[]{"OPR/([0-9]+)", "Opera/([0-9]+)", "Version/([0-9]+)"}),

    APPLE_WEB_KIT("Apple WebKit", new String[]{"AppleWebKit"}, null),
    APPLE_ITUNES("iTunes", new String[]{"iTunes"}, null),
    APPLE_APPSTORE("App Store", new String[]{"MacAppStore"}, null),
    ADOBE_AIR("Adobe AIR application", new String[]{"AdobeAIR"}, null),

    MOZILLA("Mozilla", new String[]{"Mozilla", "Moozilla"}, null),
    THUNDERBIRD("Thunderbird", new String[]{"Thunderbird"}, new String[]{"Thunderbird/([0-9]+))"}),

    BOT("Robot/Spider", new String[]{"Googlebot", "Mediapartners-Google", "Web Preview", "bot", "Applebot", "spider", "crawler", "Feedfetcher", "Slurp", "Twiceler", "Nutch", "BecomeBot", "bingbot", "BingPreview", "Google Web Preview", "WordPress.com mShots", "Seznam", "facebookexternalhit", "YandexMarket", "Teoma", "ThumbSniper", "Phantom", "Go-http-client", "Java/", "python-requests", "YandexBot", "AdsBot-Google", "AhrefsBot"}, null),
    BOT_MOBILE("Mobile Robot/Spider", new String[]{"Googlebot-Mobile"}, null),

    KONQUEROR("Konqueror", new String[]{"Konqueror"}, null),
    DOLFIN2("Samsung Dolphin 2", new String[]{"Dolfin/2"}, null),
    LOTUS_NOTES("Lotus Notes", new String[]{"Lotus-Notes"}, null),
    CAMINO("Camino", new String[]{"Camino"}, new String[]{"Camino/([0-9]+)"}),
    FLOCK("Flock", new String[]{"Flock"}, null),

    VIVALDI("Vivaldi", new String[]{"Vivaldi"}, null),
    SEAMONKEY("SeaMonkey", new String[]{"SeaMonkey"}, null),

    CFNETWORK("CFNetwork", new String[]{"CFNetwork"}, null),
    EUDORA("Eudora", new String[]{"Eudora", "EUDORA"}, null),
    POCOMAIL("PocoMail", new String[]{"PocoMail"}, null),
    THEBAT("The Bat!", new String[]{"The Bat"}, null),
    NETFRONT("NetFront", new String[]{"NetFront"}, null),
    EVOLUTION("Evolution", new String[]{"CamelHttpStream"}, null),
    LYNX("Lynx", new String[]{"Lynx"}, null),
    OMNIWEB("Omniweb", new String[]{"OmniWeb"}, null),
    BLACKBERRY10("BlackBerry", new String[]{"BB10"}, null),
    SILK("Silk", new String[]{"Silk/"}, null),
    DOWNLOAD("Downloading Tool", new String[]{"cURL", "wget", "ggpht.com", "Apache-HttpClient"}, null),
    MOBILE("Mobile", new String[]{"Mobile"}, new String[]{"Version/([0-9])", "Version ([0-9])"}),
    UNKNOWN("Unknown", new String[0], null);

    private final String name;

    private final String[] versionRegexs;
    private final String[] aliases;

    private final String[] excludeList;


    TKClientBrowser(String name, String[] aliases, String[] versionRegexs) {
        this(name, aliases, versionRegexs, null);
    }

    TKClientBrowser(String name, String[] aliases, String[] versionRegexs, String[] exclude) {
        this.name = name;
        this.aliases = aliases;
        this.versionRegexs = versionRegexs;
        this.excludeList = exclude;
    }

    public String getName() {
        return this.name;
    }

    public static String getTitle(String agentString) {
        TKClientBrowser browser = TKClientBrowser.parse(agentString);
        return browser.getName() + " " + browser.getVersion(agentString);
    }


    public int getVersion(String agentString) {
        if (this.versionRegexs != null) {
            Pattern pattern;
            Matcher matcher;
            for (String regex : versionRegexs) {
                pattern = Pattern.compile(regex);
                matcher = pattern.matcher(agentString);
                if (matcher.find()) {
                    return TKConvert.toInteger(matcher.group(1));
                }
            }
        }
        return 1;
    }

    private static TKClientBrowser parse(String agentString) {
        if (agentString != null) {
            agentString = agentString.toLowerCase();
            for (TKClientBrowser match : TKClientBrowser.values()) {
                if (match.contains(agentString, match.aliases)) {
                    if (!match.contains(agentString, match.excludeList)){
                        return match;
                    }
                }
            }
        }
        return UNKNOWN;
    }

    private boolean contains(String str, String[] inArr) {
        if (inArr != null && str != null) {
            for (String inStr : inArr) {
                if (str.toLowerCase().contains(inStr.toLowerCase())) {
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        String agentString = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko ";
        System.out.println(TKClientBrowser.getTitle(agentString));
    }
}
